from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.renderers import JSONRenderer

from luckin.authentication import TokenAuthentication
from luckin.service.order import GoodsOrderService
from luckin.serialization import GoodsOrderSerializers


class GoodsOrderView(APIView):
    renderer_classes = [JSONRenderer]
    authentication_classes = [TokenAuthentication]

    def get(self, request, *args, **kwargs):
        response_data = {
            "code": 200,
            "message": "success"
        }
        goods_order = GoodsOrderService()
        flag, data = goods_order.get(request)
        if flag:
            ser = GoodsOrderSerializers(data, many=True)
            response_data["data"] = ser.data
        else:
            response_data["code"] = 400
            response_data["message"] = data

        return Response(status=200, content_type="application/json", data=response_data)

    def post(self, request, *args, **kwargs):
        response_data = {
            "code": 200,
            "message": "success"
        }
        goods_order = GoodsOrderService()
        flag, data = goods_order.post(request)
        if flag:
            response_data["data"] = data
        else:
            response_data["code"] = 400
            response_data["message"] = data

        return Response(status=200, content_type="application/json", data=response_data)

    def delete(self, request, *args, **kwargs):
        response_data = {
            "code": 200,
            "message": "success"
        }
        goods_order = GoodsOrderService()
        flag, data = goods_order.delete(request)
        if not flag:
            response_data["code"] = 400
        response_data["message"] = data

        return Response(status=200, content_type="application/json", data=response_data)
